//
//  SunAddressVc.m
//  Xspcf
//
//  Created by sunshine on 17/5/15.
//  Copyright © 2017年 小小算盘. All rights reserved.
//

#import "SunAddressVc.h"
#import "SunAddEditVC.h"

@interface SunAddressVc ()
{
    NSMutableArray *_arr;
    UIButton *_setbtn1;
    UIButton *_setbtn2;
    UIView *_view1;
    UIView *_view2;
    UIButton *_addBtn;
}
@end

@implementation SunAddressVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUND;
    self.title = @"收货地址";
    
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [_view1 removeFromSuperview];
    [_view2 removeFromSuperview];
    _view1 = nil;
    _view2 = nil;
    [self requestToAddress];
}

- (void)creatUI{
    if (!_addBtn) {
        _addBtn = [[UIButton alloc]init];
        _addBtn = [[UIButton alloc]init];
        [self.view addSubview:_addBtn];
        [_addBtn lt_Bottom:SunEdgesMake(X(30), Y(70), X(30), Y(44)) to:nil save:YES];
        _addBtn.clipsToBounds = YES;
        [_addBtn setBackgroundColor:BLUE forState:UIControlStateNormal];
        [_addBtn setBackgroundColor:[HXColor colorWithHexString:@"87CEFA"] forState:UIControlStateHighlighted];
        _addBtn.layer.cornerRadius = 4;
        [_addBtn setTitle:@"添加地址" forState:UIControlStateNormal];
        [_addBtn setTitleColor:WHITE forState:UIControlStateNormal];
        _addBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [_addBtn addTarget:self action:@selector(addAddress) forControlEvents:UIControlEventTouchUpInside];
    }
    _addBtn.hidden = NO;
    int len = (int)_arr.count;
    if (len >= 1) {
        _view1 = [[UIView alloc]init];
        [self.view addSubview:_view1];
        [_view1 lt_setLeft:0 to:nil save:YES];
        [_view1 lt_setRight:0 to:nil save:YES];
        [_view1 lt_setTop:Y(20) to:nil save:YES];
        _view1.backgroundColor = WHITE;
        UILabel *name = [[UILabel alloc]init];
        [_view1 addSubview:name];
        [name lt_setTop:Y(16) to:nil save:YES];
        [name lt_setLeft:X(25) to:nil save:YES];
        name.text = [_arr[0] objectForKey:@"consignee"];
        name.font = FONT(16);
        name.textColor = BLACK;
        UILabel *phone = [[UILabel alloc]init];
        [_view1 addSubview:phone];
        [phone lt_YcenterTo:name save:YES];
        [phone lt_setLeft:X(30) to:name save:YES];
        phone.text = [Sun hidePhone:[_arr[0] objectForKey:@"mobile"]];
        phone.font = FONT(16);
        phone.textColor = BLACK;
        UILabel *tag = [[UILabel alloc]init];
        [_view1 addSubview:tag];
        [tag lt_bottomEqual:name save:YES];
        [tag lt_setRight:X(25) to:nil save:YES];
        tag.font = FONT(13);
        tag.textColor = BLUE;
        tag.text = [_arr[0] objectForKey:@"labels"];
        UILabel *add = [[UILabel alloc]init];
        [_view1 addSubview:add];
        [add lt_leftEqual:name save:YES];
        [add lt_setTop:Y(10) to:name save:YES];
        [add lt_setRight:X(25) to:nil save:YES];
        add.numberOfLines = 0;
        add.font = FONT(16);
        add.textColor = BLACK;
        add.text = [[_arr[0] objectForKey:@"detail"] stringByAppendingString:[_arr[0] objectForKey:@"address"]];
        UIView *line = [[UIView alloc]init];
        [_view1 addSubview:line];
        [line lt_Top:SunEdgesMake(Y(10), X(25), X(25), 0.5) to:add save:YES];
        line.backgroundColor = LINE;
        _setbtn1 = [[UIButton alloc]init];
        [_view1 addSubview:_setbtn1];
        [_setbtn1 lt_setLeft:X(25) to:nil save:YES];
        [_setbtn1 lt_setTop:X(10) to:line save:YES];
        [_setbtn1 setTitle:@"默认地址" forState:UIControlStateNormal];
        _setbtn1.titleLabel.font = FONT(13);
        [_setbtn1 setTitleEdgeInsets:UIEdgeInsetsMake(0, X(3), 0, -3)];
        _setbtn1.tag = [[_arr[0] objectForKey:@"id"]intValue];
        [_setbtn1 addTarget:self action:@selector(setDefault:) forControlEvents:UIControlEventTouchUpInside];
        if ([[_arr[0] objectForKey:@"is_default"]intValue] == 1) {
            [_setbtn1 setImage:[UIImage imageNamed:@"address_select"] forState:UIControlStateNormal];
            [_setbtn1 setTitleColor:RED forState:UIControlStateNormal];
        }else {
            [_setbtn1 setImage:[UIImage imageNamed:@"address_default"] forState:UIControlStateNormal];
            [_setbtn1 setTitleColor:BLACK forState:UIControlStateNormal];
        }
        UIButton *btn1 = [[UIButton alloc]init];
        [_view1 addSubview:btn1];
        [btn1 lt_YcenterTo:_setbtn1 save:YES];
        [btn1 lt_setRight:X(100) to:nil save:YES];
        [btn1 setImage:[UIImage imageNamed:@"address_edit"] forState:UIControlStateNormal];
        [btn1 setTitle:@"编辑" forState:UIControlStateNormal];
        [btn1 setTitleColor:BLACK forState:UIControlStateNormal];
        btn1.titleLabel.font = FONT(13);
        [btn1 setTitleEdgeInsets:UIEdgeInsetsMake(0, X(3), 0, -3)];
        [btn1 addTarget:self action:@selector(editAddress:) forControlEvents:UIControlEventTouchUpInside];
        btn1.tag = 1;
//        btn1.tag = [[_arr[1] objectForKey:@"id"]intValue];
        UIButton *btn2 = [[UIButton alloc]init];
        [_view1 addSubview:btn2];
        [btn2 lt_YcenterTo:_setbtn1 save:YES];
        [btn2 lt_setRight:X(25) to:nil save:YES];
        [btn2 setImage:[UIImage imageNamed:@"address_delete"] forState:UIControlStateNormal];
        [btn2 setTitle:@"删除" forState:UIControlStateNormal];
        [btn2 setTitleColor:BLACK forState:UIControlStateNormal];
        btn2.titleLabel.font = FONT(13);
        [btn2 setTitleEdgeInsets:UIEdgeInsetsMake(0, X(3), 0, -3)];
        [btn2 addTarget:self action:@selector(deleteAddress:) forControlEvents:UIControlEventTouchUpInside];
        btn2.tag = 1;
//        btn2.tag = [[_arr[1] objectForKey:@"id"]intValue];
        [_setbtn1 lt_setBottom:Y(10) to:nil save:YES];
    }
    if (len >= 2) {
        _addBtn.hidden = YES;
        _view2 = [[UIView alloc]init];
        [self.view addSubview:_view2];
        [_view2 lt_setLeft:0 to:nil save:YES];
        [_view2 lt_setRight:0 to:nil save:YES];
        [_view2 lt_setTop:Y(20) to:_view1 save:YES];
        _view2.backgroundColor = WHITE;
        UILabel *name = [[UILabel alloc]init];
        [_view2 addSubview:name];
        [name lt_setTop:Y(16) to:nil save:YES];
        [name lt_setLeft:X(25) to:nil save:YES];
        name.text = [_arr[1] objectForKey:@"consignee"];
        name.font = FONT(16);
        name.textColor = BLACK;
        UILabel *phone = [[UILabel alloc]init];
        [_view2 addSubview:phone];
        [phone lt_YcenterTo:name save:YES];
        [phone lt_setLeft:X(30) to:name save:YES];
        phone.text = [Sun hidePhone:[_arr[1] objectForKey:@"mobile"]];
        phone.font = FONT(16);
        phone.textColor = BLACK;
        UILabel *tag = [[UILabel alloc]init];
        [_view2 addSubview:tag];
        [tag lt_bottomEqual:name save:YES];
        [tag lt_setRight:X(25) to:nil save:YES];
        tag.font = FONT(13);
        tag.textColor = BLUE;
        tag.text = [_arr[1] objectForKey:@"labels"];
        UILabel *add = [[UILabel alloc]init];
        [_view2 addSubview:add];
        [add lt_leftEqual:name save:YES];
        [add lt_setTop:Y(10) to:name save:YES];
        [add lt_setRight:X(25) to:nil save:YES];
        add.numberOfLines = 0;
        add.font = FONT(16);
        add.textColor = BLACK;
        add.text = [[_arr[1] objectForKey:@"detail"] stringByAppendingString:[_arr[1] objectForKey:@"address"]];
        UIView *line = [[UIView alloc]init];
        [_view2 addSubview:line];
        [line lt_Top:SunEdgesMake(Y(10), X(25), X(25), 0.5) to:add save:YES];
        line.backgroundColor = LINE;
        _setbtn2 = [[UIButton alloc]init];
        [_view2 addSubview:_setbtn2];
        [_setbtn2 lt_setLeft:X(25) to:nil save:YES];
        [_setbtn2 lt_setTop:X(10) to:line save:YES];
        [_setbtn2 setTitle:@"默认地址" forState:UIControlStateNormal];
        _setbtn2.titleLabel.font = FONT(13);
        [_setbtn2 setTitleEdgeInsets:UIEdgeInsetsMake(0, X(3), 0, -3)];
        _setbtn2.tag = [[_arr[1] objectForKey:@"id"]intValue];
        [_setbtn2 addTarget:self action:@selector(setDefault:) forControlEvents:UIControlEventTouchUpInside];
        if ([[_arr[1] objectForKey:@"is_default"]intValue] == 1) {
            [_setbtn2 setImage:[UIImage imageNamed:@"address_select"] forState:UIControlStateNormal];
            [_setbtn2 setTitleColor:RED forState:UIControlStateNormal];
        }else {
            [_setbtn2 setImage:[UIImage imageNamed:@"address_default"] forState:UIControlStateNormal];
            [_setbtn2 setTitleColor:BLACK forState:UIControlStateNormal];
        }
        UIButton *btn1 = [[UIButton alloc]init];
        [_view2 addSubview:btn1];
        [btn1 lt_YcenterTo:_setbtn2 save:YES];
        [btn1 lt_setRight:X(100) to:nil save:YES];
        [btn1 setImage:[UIImage imageNamed:@"address_edit"] forState:UIControlStateNormal];
        [btn1 setTitle:@"编辑" forState:UIControlStateNormal];
        [btn1 setTitleColor:BLACK forState:UIControlStateNormal];
        btn1.titleLabel.font = FONT(13);
        [btn1 setTitleEdgeInsets:UIEdgeInsetsMake(0, X(3), 0, -3)];
        [btn1 addTarget:self action:@selector(editAddress:) forControlEvents:UIControlEventTouchUpInside];
        btn1.tag = 2;
//        btn1.tag = [[_arr[1] objectForKey:@"id"]intValue];
        UIButton *btn2 = [[UIButton alloc]init];
        [_view2 addSubview:btn2];
        [btn2 lt_YcenterTo:_setbtn2 save:YES];
        [btn2 lt_setRight:X(25) to:nil save:YES];
        [btn2 setImage:[UIImage imageNamed:@"address_delete"] forState:UIControlStateNormal];
        [btn2 setTitle:@"删除" forState:UIControlStateNormal];
        [btn2 setTitleColor:BLACK forState:UIControlStateNormal];
        btn2.titleLabel.font = FONT(13);
        [btn2 setTitleEdgeInsets:UIEdgeInsetsMake(0, X(3), 0, -3)];
        [btn2 addTarget:self action:@selector(deleteAddress:) forControlEvents:UIControlEventTouchUpInside];
        btn2.tag = 2;
//        btn2.tag = [[_arr[1] objectForKey:@"id"]intValue];
        [_setbtn2 lt_setBottom:Y(10) to:nil save:YES];
    }
    if (len < 2) {
        ;
    }
}

#pragma mark click
- (void)setDefault:(UIButton *)btn{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_DEFAULTADDRESS];
    [HttpRequest postURL:url withParam:@{@"id":[NSString stringWithFormat:@"%ld",(long)btn.tag]} success:^(id requestDic, NSString *msg) {
        if (btn == _setbtn1) {
            [_setbtn1 setImage:[UIImage imageNamed:@"address_select"] forState:UIControlStateNormal];
            [_setbtn1 setTitleColor:RED forState:UIControlStateNormal];
            [_setbtn2 setImage:[UIImage imageNamed:@"address_default"] forState:UIControlStateNormal];
            [_setbtn2 setTitleColor:BLACK forState:UIControlStateNormal];
        }else {
            [_setbtn2 setImage:[UIImage imageNamed:@"address_select"] forState:UIControlStateNormal];
            [_setbtn2 setTitleColor:RED forState:UIControlStateNormal];
            [_setbtn1 setImage:[UIImage imageNamed:@"address_default"] forState:UIControlStateNormal];
            [_setbtn1 setTitleColor:BLACK forState:UIControlStateNormal];
        }
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun] showMsg:errorInfo];
    }];
}

- (void)editAddress:(UIButton *)btn{
    SunAddEditVC *vc = [[SunAddEditVC alloc]init];
    vc.address = _arr[btn.tag - 1];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)deleteAddress:(UIButton *)btn{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_DELADDRESS];
    [HttpRequest postURL:url withParam:@{@"id":_arr[btn.tag-1][@"id"]} success:^(id requestDic, NSString *msg) {
        [_view1 removeFromSuperview];
        [_view2 removeFromSuperview];
        _view1 = nil;
        _view2 = nil;
        [_arr removeObjectAtIndex:btn.tag-1];
        [self creatUI];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun] showMsg:errorInfo];
    }];
}

- (void)addAddress{
    SunAddEditVC *vc = [[SunAddEditVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark request
- (void)requestToAddress{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_GETADDRESS];
    [HttpRequest postURL:url withParam:nil success:^(id requestDic, NSString *msg) {
        _arr = [NSMutableArray arrayWithArray:requestDic];
        [self creatUI];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun] showMsg:errorInfo];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
